package com.dragon.manage.action;

import java.io.File;
import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSONObject;
import com.dragon.manage.bean.ManageUser;
import com.dragon.manage.service.IManageService;
import com.dragon.manage.util.EncryUtil;
import com.dragon.manage.util.LogUtil;
import com.dragon.manage.util.ManageUtil;
import com.dragon.manage.util.RechargeUtil;
import com.dragon.manage.util.StaticData;
import com.dragon.manage.util.VerifyCodeUtils;
import com.opensymphony.xwork2.ActionContext;
@SuppressWarnings("rawtypes")
public class ManageAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	private IManageService manageService;
	
	public IManageService getManageService() {
		return manageService;
	}
	public void setManageService(IManageService manageService) {
		this.manageService = manageService;
	}
	/**
	 * 获取验证码
	 */
	public String  getCode(){
		String reStr="";
		//生成随机字串
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
		//存入会话session
		HttpSession session = request.getSession(true);
		session.setAttribute("rand"+session.getId(), verifyCode.toLowerCase());
		//生成图片
		int w = 200, h = 80;
		try {
			String imageName = RechargeUtil.encoderByMd5(verifyCode);
			/*
			 * 生成验证码
			 */
			File file = new File(request.getSession().getServletContext().getRealPath("/")+"/images/jpeg/"+
					imageName+".jpg");
			VerifyCodeUtils.outputImage(w, h, file, verifyCode);
			
			ActionContext ct=ActionContext.getContext();
			JSONObject result = new JSONObject();
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
			result.put("imageName",imageName);
			ct.put(StaticData.reKey.result.getKey(), result);
		} catch (Exception e) {
			LogUtil.error("获取验证码失败", e);
			ActionContext ct=ActionContext.getContext();
			JSONObject result = new JSONObject();
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			reStr=StaticData.RE_SUCCESS;
			return reStr;
		}
		 reStr=StaticData.RE_SUCCESS;
		return reStr;
	}
	/**
	 * 管理员登录
	 * @return
	 */
	public String manageLogin(){
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();

		try {
			/*
			 * 获取对应的参数
			 */
			String userName=request.getParameter("userName");
			String pwd=request.getParameter("pwd");
			String verify=request.getParameter("verify");
			HttpSession sessionH = request.getSession(true);
			if(userName==null||pwd==null||verify==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			/*
			 * 验证验证码
			 */
			Object vCode = sessionH.getAttribute("rand"+sessionH.getId());
			if(vCode==null||!vCode.equals(verify.toLowerCase())){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.verify_error.getType());
			}else{
				/*
				 * 登录
				 */
				ManageUser manageUser = manageService.manageUserLogin(userName, pwd,
						request.getRemoteAddr());
				if(manageUser!=null){
					Cookie cookie = new Cookie(StaticData.SESSION_ID_KEY,
							EncryUtil.encrypt(manageUser.getId()+""));
					cookie.setMaxAge(StaticData.cookie_times); 
					response.addCookie(cookie);
					result.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
					result.put(StaticData.reKey.userName.getKey(), manageUser.getUserName());
					result.put(StaticData.reKey.userRight.getKey(), manageUser.getUserRight());
				}else{
					result.put(StaticData.reKey.code.getKey(), StaticData.codeType.not_is_user.getType());
				}
			}
			
			ct.put(StaticData.reKey.result.getKey(), result);
		} catch (Exception e) {
			LogUtil.error("管理员登录异常", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
		
		return StaticData.RE_SUCCESS;
	}

	/**
	 * 获取管理用户信息
	 * @return
	 * @throws Exception
	 */
	public String getManageUser() throws Exception{
		/*
		 * 容器和返回的json值
		 */
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 在cookies中获取用户id
			 */
			int id = ManageUtil.getManageUserId(request);
			if(id==StaticData.no_param){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.user_not_login.getType());
				return StaticData.RE_SUCCESS;
			}
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser = manageService.getManageUser(id);
			if(manageUser==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.not_is_user.getType());
				return StaticData.RE_SUCCESS;
			}
			/*
			 * 返回对应的前端信息
			 */
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
			result.put(StaticData.reKey.userName.getKey(), manageUser.getUserName());
			result.put(StaticData.reKey.userRight.getKey(), manageUser.getUserRight());
			ct.put(StaticData.reKey.result.getKey(), result);
		
		} catch (Exception e) {
			LogUtil.error("获取管理员用户信息异常", e);
			ct.put(StaticData.reKey.result.getKey(), StaticData.codeType.other_error.getType());
			return StaticData.RE_SUCCESS;
		}
		return StaticData.RE_SUCCESS;
	}
	/**
	 * 用户退出登录
	 * @return
	 * @throws Exception
	 */
	public String logOutManageUer(){
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		String reStr=StaticData.RE_SUCCESS;
		session.clear();
		result.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
		ct.put(StaticData.reKey.result.getKey(), result);
		return reStr;
	}
	
	/**
	 * 更新密码
	 * @return
	 */
	public String updateManageUserPwd(){
		/*
		 * 容器和返回的json值
		 */
		long startTime = new Date().getTime();
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		String reStr=StaticData.RE_SUCCESS;
		int code = StaticData.codeType.ok.getType();
		try {
			String orldPwd=request.getParameter("oldPwd");
			String newPwd=request.getParameter("newPwd");
			int id =ManageUtil.getManageUserId(request);
			if(id==StaticData.no_param){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.user_not_login.getType());
				return reStr;
			}
			code = manageService.updateManageUserPwd(id, orldPwd, newPwd);
			result.put(StaticData.reKey.code.getKey(), code);
			ct.put(StaticData.reKey.result.getKey(), result);
		} catch (Exception e) {
			LogUtil.error("修改密码异常", e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			return reStr;
		}finally{
			long endTime = new Date().getTime();
			LogUtil.error((endTime-startTime)+" ");
		}
		return reStr;
	}
}
